#!/bin/bash

PEAC_VERSION=0.1

PEAC_SYSTEM_INFO=""

PEAC_CONF_FILE="peac.cf"
PEAC_OPT_MASTER_HOST=`hostname -f`
PEAC_OPT_NICE="nice -n 15 "
PEAC_OPT_MAKE_OPTS="-j`cat /proc/cpuinfo | grep processor | wc -l`"
PEAC_OPT_CONFIGURE_OPTS="--disable-mysql"
PEAC_OPT_CURRENT_ROOT_VER="v5-28-00d"

PEAC_OPT_SW_XROOTD_PORT="11094"

# PEAC_OPT_POD_VER="PoD-3.4"
# PEAC_OPT_POD_URL="http://pod.gsi.de/releases/pod/3.4/PoD-3.4-Source.tar.gz"
PEAC_OPT_POD_VER="PoD-3.5.51.gf6be4-Source"
PEAC_OPT_POD_URL="http://pod.gsi.de/releases/pod/nightly/$PEAC_OPT_POD_VER.tar.gz"
PEAC_OPT_POD_WK_CF="pod_ssh.cf"


PEAC_XROOTD_VER="3.0.3"
PEAC_XROOTD_URL="http://xrootd.org/download/v3.0.3/xrootd-$PEAC_XROOTD_VER.tar.gz"

function CheckSystemInfo() {

  mkdir -p /tmp/$USER-ccache
  if [ ! -h ~/.ccache ];then 
    rm -Rf ~/.ccache
    ln -s /tmp/$USER-ccache ~/.ccache
  fi

  local MY_SYSTEM_STRING="unsupported"
  local MY_RELEASE=`cat /etc/*-release`
  local MY_GCC_VER=`gcc -v 2>&1 | tail -1 | awk '{print $3}'`
  local MY_PLATFORM=`uname -m`
  local MY_RELEASE_FIRST_STR=`echo $MY_RELEASE | awk '{print $1}' | head -n 1`
  if [ "$MY_RELEASE_FIRST_STR" == "Gentoo" ];then
    MY_SYSTEM_STRING="gentoo"
  elif [ "$MY_RELEASE_FIRST_STR" == "Scientific" ];then
    MY_SYSTEM_STRING="sl"
  elif [ "$MY_RELEASE_FIRST_STR" == "DISTRIB_ID=Ubuntu" ];then
    MY_SYSTEM_STRING="ubuntu"
  elif [ "$MY_RELEASE_FIRST_STR" == "Fedora" ];then
    MY_SYSTEM_STRING="fedora"
  fi

  MY_SYSTEM_STRING="$MY_SYSTEM_STRING-$MY_PLATFORM-$MY_GCC_VER"

  echo "$MY_SYSTEM_STRING"
}

PEAC_SYSTEM_INFO=$(CheckSystemInfo)

function ShowHelp() {
  echo "./peac-installer <options>"
  echo "  version : $PEAC_VERSION"
  echo "  system : $PEAC_SYSTEM_INFO"
  echo ""
  echo "  --help                        : shows help"
  echo ""
  echo "  --install-server              : Installs server"
  echo "  --all-in-one                  : An action for PoD SERVER and WORKERS <start|stop|restart>"
  echo ""
  echo "  --generate-pod-workers-config : Generated pod config file"

  echo ""
}

function CreateDir() {
  if [ ! -d $1 ];then
    mkdir -p $1
  fi
}

function CheckPEACConfigFile() {
  if [ ! -f $PEAC_CONF_FILE ]; then
    echo "Creating default $PEAC_CONF_FILE file ..."
    echo "# generated `date`" > $PEAC_CONF_FILE
  fi
  
  AddVariableToConfig PEAC_SYS_DIR `pwd`
  AddVariableToConfig PEAC_SYS_ROOTSYS_SHARED ""
  AddVariableToConfig PEAC_SYS_INIT_SCRIPT ""


  AddVariableToConfig MLSENSOR_TARGETML "voalice12.cern.ch" "c"
  AddVariableToConfig MLSENSOR_CLUSTERNAME "PEAC::CERN::FIXME" "c"


  echo ""

  CheckExecutable svn
  CheckExecutable cmake

  source $PEAC_CONF_FILE

  PEAC_OPT_LOG_DIR="$PEAC_SYS_DIR/logs"
  PEAC_OPT_TMP_DIR="$PEAC_SYS_DIR/tmp"
  PEAC_OPT_SW_DIR="$PEAC_SYS_DIR/sw"
  PEAC_OPT_SW_BINARY_DIR="$PEAC_OPT_SW_DIR/binary"
  PEAC_OPT_SW_DIR_ROOT="$PEAC_OPT_SW_DIR/ROOT"
  PEAC_OPT_SW_LIST_FILE="$PEAC_OPT_SW_DIR/sw-list.txt"
  PEAC_OPT_SW_POD_DIR="$PEAC_OPT_SW_DIR/$PEAC_SYSTEM_INFO/PoD"
  PEAC_OPT_POD_SYS_DIR="$PEAC_SYS_DIR/PoD"
  PEAC_OPT_MLSENSOR_DIR="$PEAC_OPT_SW_DIR/MLSensor"


  CreateDir $PEAC_OPT_SW_DIR
  CreateDir $PEAC_OPT_SW_BINARY_DIR
  CreateDir $PEAC_OPT_LOG_DIR
  echo "$PEAC_SYS_DIR"
}

function AddVariableToConfig() {
  if [ -z "$3" ];then
    echo "$1=\"$2\""
  fi

  MYTMP=`cat $PEAC_CONF_FILE | grep $1`

  if [ -z "$MYTMP" ];then
    echo "" >> $PEAC_CONF_FILE
    if [ -z "$3" ];then
    echo "$1=\"$2\""
      echo "export $1=\"$2\"" >> $PEAC_CONF_FILE
    else
      echo "# export $1=\"$2\"" >> $PEAC_CONF_FILE
    fi
  fi
  
}

function CheckExecutable() {
  if [ -z $1 ];then 
    return 0
  fi
  TMP=`which $1`
  if [ "$?" != "0" ];then
    echo "Executable $1 was not found !!!"
    echo "Tip: Install it !!!"
    exit 1
  fi

}

InstallPackage() {
  local MY_DOWNLOAD_TYPE="$1"
  local MY_NAME="$2"
  local MY_VER="$3"
  local MY_URL="$4"
  local MY_PREFIX="$5"

  local MY_OPT="$5"
  
  if [ "$MY_OPT" == "force" ];then
    rm -Rf $MY_PREFIX
  fi

  if [ -f $MY_PREFIX/peac-env.sh ];then
    echo "Package $MY_NAME $MY_VER is already installed at $MY_PREFIX"
    return
  fi

  CreateDir $MY_PREFIX
  local MY_TMP_DIR="$PEAC_OPT_TMP_DIR/$MY_NAME"

  CreateDir $MY_TMP_DIR

  MY_PWD="`pwd`"
  cd $MY_TMP_DIR
  
  local MY_NAME_VER=`basename $MY_URL`


  echo "Downloading $MY_NAME using $MY_DOWNLOAD_TYPE from $MY_URL ..."
  if [ "$MY_DOWNLOAD_TYPE" == "wget" ];then
    if [ ! -f $MY_NAME_VER ];then
	$MY_DOWNLOAD_TYPE $MY_URL >& ${MY_DOWNLOAD_TYPE}_${MY_NAME}-${MY_VER}.out
	echo "Unpacking $MY_NAME with version $MY_VER ..."
	MY_NAME_VER_NO_TARGZ=${MY_NAME_VER%.tar.gz}
	rm -Rf $MY_NAME_VER_NO_TARGZ
	tar xfz $MY_NAME_VER
	MY_NAME_VER=${MY_NAME_VER%.tar.gz}
    fi
  elif [ "$MY_DOWNLOAD_TYPE" == "svn" ];then
    $MY_DOWNLOAD_TYPE co $MY_URL $MY_VER >& ${MY_DOWNLOAD_TYPE}_${MY_NAME}-${MY_VER}.out
  else
    echo "not supported download plugin (wget or svn)"
    exit 1;
  fi

  cd $MY_NAME_VER
  Install_$MY_NAME $MY_PREFIX `pwd`

  cd $MY_PREFIX
  echo "Creating tarball $PEAC_OPT_SW_BINARY_DIR/$PEAC_SYSTEM_INFO-$MY_NAME-$MY_VER.tar.gz ..."
  tar cfz $PEAC_OPT_SW_BINARY_DIR/$PEAC_SYSTEM_INFO-$MY_NAME-$MY_VER.tar.gz .
  
  cd $MY_TMP_DIR
  echo "Cleaning tmp files ..."
  rm -Rf $MY_NAME_VER
  cd $MY_PWD

}

function Install_xrootd() {

  echo "Compiling and installing xrootd $PEAC_XROOTD_VER ..."
  ./configure.classic --prefix=$1 >& $1/configure.out

  $PEAC_OPT_NICE make $PEAC_OPT_MAKE_OPTS >& $1/make.out
  if [ "$?" != "0" ]; then
    echo "Error in make of xrootd $PEAC_XROOTD_VER in `pwd`"
    exit 10
  fi

  $PEAC_OPT_NICE make $PEAC_OPT_MAKE_OPTS install >& $1/make_install.out
  if [ "$?" != "0" ]; then
    echo "Error in make install of xrootd $PEAC_XROOTD_VER in `pwd`"
    exit 11
  fi
  
  local MY_XRD_PLATFORM=`ls $1/lib`

cat > $1/peac-env.sh <<EOF
#/bin/bash

export PATH="$1/bin/$MY_XRD_PLATFORM:\$PATH"
export LD_LIBRARY_PATH="$1/lib/$MY_XRD_PLATFORM:\$LD_LIBRARY_PATH"

EOF
  
}

function Install_pod() {
  
  mkdir -p $2/build
  cd $2/build
  $PEAC_OPT_NICE cmake -C ../BuildSetup.cmake -DCMAKE_INSTALL_PREFIX="$1" ../ >& $1/configure.out
  if [ "$?" != "0" ]; then
    echo "Error in configuration of PoD $PEAC_OPT_POD_VER in `pwd`"
    exit 10
  fi

  $PEAC_OPT_NICE make $PEAC_OPT_MAKE_OPTS install >& $1/make_install.out
  if [ "$?" != "0" ]; then
    echo "Error in compilation of PoD $PEAC_OPT_POD_VER in `pwd`" 
    exit 11
  fi
  rm -Rf ~/.PoD $PEAC_OPT_POD_SYS_DIR
  touch $1/peac-env.sh
}

function Install_root() {
  
  echo "Compiling and installing root to $1 ..."
  ./configure $PEAC_OPT_CONFIGURE_OPTS >& $1/configure.out
#   ./configure $PEAC_OPT_CONFIGURE_OPTS --prefix=$1 >& $1/configure.out

  $PEAC_OPT_NICE make $PEAC_OPT_MAKE_OPTS >& $1/make.out
  if [ "$?" != "0" ]; then
    echo "Error in make of root in `pwd`"
    exit 10
  fi
  mv * $1
#   $PEAC_OPT_NICE make $PEAC_OPT_MAKE_OPTS install >& $1/make_install.out
#   if [ "$?" != "0" ]; then
#     echo "Error in make install of root in `pwd`"
#     exit 11
#   fi

  

  echo "source $1/bin/thisroot.sh" >  $1/peac-env.sh
}

function InstallRootFromVersion() {
  local MY_ROOT_VER="$1"
  local MY_ROOT_VER_SVN=""
  if [ -z $MY_ROOT_VER ];then
    MY_ROOT_VER_SVN="http://root.cern.ch/svn/root/tags/$PEAC_OPT_CURRENT_ROOT_VER"
    MY_ROOT_VER=`basename $MY_ROOT_VER_SVN`
  fi

  if [ "$MY_ROOT_VER" == "trunk" ]; then
    MY_ROOT_VER_SVN="http://root.cern.ch/svn/root/trunk"
    MY_ROOT_VER=`basename $MY_ROOT_VER_SVN`
  fi
  local MY_TMP=`echo "$MY_ROOT_VER" | awk -F '-' '{print $NF}'`
  if [ $MY_TMP == "patches" ]; then
    MY_ROOT_VER_SVN="http://root.cern.ch/svn/root/branches/$MY_ROOT_VER"
    MY_ROOT_VER=`basename $MY_ROOT_VER_SVN`
  fi

  if [ -z $MY_ROOT_VER_SVN ]; then
    MY_ROOT_VER_SVN="http://root.cern.ch/svn/root/tags/$MY_ROOT_VER"
    MY_ROOT_VER=`basename $MY_ROOT_VER_SVN`
  fi

  InstallPackage svn root $MY_ROOT_VER $MY_ROOT_VER_SVN $PEAC_OPT_SW_DIR/$PEAC_SYSTEM_INFO/root/$MY_ROOT_VER/ $2

}

function DoPoDWorkersAction() {

#   if [ -n "$2" ];then
#     PEAC_OPT_CURRENT_ROOT_VER="$2"
#   fi
  local MY_CMD=$1
  if [ "$MY_CMD" == "start" ]; then
    MY_CMD="--submit"
  fi
  if [ "$MY_CMD" == "stop" ]; then
    MY_CMD="--clean"
  fi

  if [ ! -f $PEAC_OPT_SW_DIR/$PEAC_SYSTEM_INFO/root/$PEAC_OPT_CURRENT_ROOT_VER/peac-env.sh ];then
    echo "ROOT $PEAC_OPT_SW_DIR/$PEAC_SYSTEM_INFO/root/$PEAC_OPT_CURRENT_ROOT_VER/peac-env.sh is not valid !!!"
    exit 100
  fi
  source $PEAC_OPT_SW_DIR/$PEAC_SYSTEM_INFO/root/$PEAC_OPT_CURRENT_ROOT_VER/peac-env.sh

  if [ -z $ROOTSYS ];then
      echo "ROOT is invalid or doesn't exist !!!!"
      exit 101
  fi 

  if [ ! -f $PEAC_OPT_SW_POD_DIR/$PEAC_OPT_POD_VER/PoD_env.sh ];then
    echo "Pod is not installed !!!"
    exit 102
  fi

  source $PEAC_OPT_SW_POD_DIR/$PEAC_OPT_POD_VER/PoD_env.sh
  pod-ssh -c $PEAC_OPT_POD_WK_CF $MY_CMD

  if [ "$MY_CMD" == "--submit" ];then
    echo ""
    echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++"
    echo ""
    echo "Now you can do in root: "
    echo ""
    echo "   root [] TProof::Open(\"`pod-info -c`\")"
    echo ""
    echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  fi
}

function CreateSSHWithoutPassword() {

  if [ ! -f wk.cf ];then
    echo "Create wk.cf first !!!"
    exit 1
  fi

  if [ ! -f ~/.ssh/config ];then
    echo "StrictHostKeyChecking no" > ~/.ssh/config
  else
    TMP=`cat ~/.ssh/config | grep StrictHostKeyChecking`
    if [ -z $TMP ];then
      echo "StrictHostKeyChecking no" > ~/.ssh/config
    fi
  fi

  if [ ! -f ~/.ssh/id_dsa ];then
    ssh-keygen -t dsa./p	
  fi 

  cat ~/.ssh/id_dsa.pub > ~/.ssh/authorized_keys

  for MY_HOST in `cat wk.cf | grep -v '#' `; do
    echo "Copying ssh files to $MY_HOST ..."
#     ssh $MY_HOST rm -Rf ~/.ssh
    scp -r ~/.ssh $MY_HOST:~
  done

}

function GeneratePoDSSHConfig() {
  if [ ! -f wk.cf ];then
    echo "Create wk.cf first !!!"
    exit 1
  fi

  rm -f $PEAC_OPT_POD_WK_CF
  let NUM=1
  for MY_HOST in `cat wk.cf | grep -v '#' `; do
    echo "Adding host $MY_HOST with id r$NUM"
    echo "r$NUM, $MY_HOST , , $PEAC_SYS_DIR/ ," >> $PEAC_OPT_POD_WK_CF

    if [ -z $1 ]; then
      echo "Creating dircotry $PEAC_SYS_DIR on worker $MY_HOST"
      ssh $MY_HOST mkdir -p $PEAC_SYS_DIR
    fi
    let NUM=$NUM+1
    
  done

}

function TrickPoD() {
    if [ ! -d $PEAC_OPT_POD_SYS_DIR ];then
      mv $HOME/.PoD $PEAC_OPT_POD_SYS_DIR
      ln -s $PEAC_OPT_POD_SYS_DIR $HOME/.PoD
    fi
}

function InitPoDServer() {
# PrepareSoftDistDir

if [ ! -f $PEAC_OPT_SW_POD_DIR/$PEAC_OPT_POD_VER/PoD_env.sh ];then
  echo "Pod is not installed !!!"
  exit 100
fi


cat > $PEAC_OPT_POD_SYS_DIR/PoD.cfg <<EOF
[server]
work_dir=$PEAC_OPT_POD_SYS_DIR
logfile_dir=$PEAC_OPT_POD_SYS_DIR/log
logfile_overwrite=yes
log_level=1
agent_shutdown_if_idle_for_sec=1800
agent_local_client_port_min=20000
agent_local_client_port_max=25000
xproof_ports_range_min=21001
xproof_ports_range_max=22000
agent_ports_range_min=22001
agent_ports_range_max=23000
agent_threads=5
agent_node_readbuffer=5000
packet_forwarding=auto
proof_cfg_entry_pattern=worker %host% port=%port% pref=100
[worker]
work_dir=\$POD_LOCATION/
logfile_dir=\$POD_LOCATION/
logfile_overwrite=yes
log_level=1
set_my_rootsys=yes
my_rootsys=\$ROOTSYS
agent_shutdown_if_idle_for_sec=1800
xproof_ports_range_min=21001
xproof_ports_range_max=22000
agent_node_readbuffer=5000
[lsf_plugin]
email_job_output=no
upload_job_log=no
[pbs_plugin]
upload_job_log=no
[ge_plugin]
upload_job_log=no
options_file=\$POD_LOCATION/etc/Job.oge.option
[condor_plugin]
upload_job_log=no
options_file=\$POD_LOCATION/etc/Job.condor.option
EOF

rm -f $PEAC_OPT_POD_SYS_DIR/user_xpd.cf.1
echo "if named PoDServer" >> $PEAC_OPT_POD_SYS_DIR/user_xpd.cf.1
echo " xpd.workdir $PEAC_SYS_DIR/proofbox" >> $PEAC_OPT_POD_SYS_DIR/user_xpd.cf.1
echo "fi" >> $PEAC_OPT_POD_SYS_DIR/user_xpd.cf.1

cat > $PEAC_OPT_POD_SYS_DIR/user_worker_env.sh <<EOF
#!/bin/bash

function CreateDir() {
  if [ ! -d \$1 ];then
    mkdir -p \$1
  fi
}


InstallPackage() {
  local MY_NAME="\$1"
  local MY_VER="\$2"
  local MY_URL="\$3"
  local MY_PREFIX="\$4"
  local MY_OPT="\$5"

  echo "MY_NAME=\$1 MY_VER=\$2 MY_URL=\$3 MY_PREFIX=\$4 MY_OPT=\$5"

  
  if [ "\$MY_OPT" == "force" ];then
    rm -Rf \$MY_PREFIX
  fi

  if [ -f \$MY_PREFIX/peac-env.sh ];then
    echo "Package \$MY_NAME \$MY_VER is already installed at \$MY_PREFIX"
    return
  fi

  CreateDir \$MY_PREFIX
  local MY_TMP_DIR="$PEAC_OPT_TMP_DIR/\$MY_NAME"

  CreateDir \$MY_TMP_DIR

  MY_PWD="\`pwd\`"
  cd \$MY_TMP_DIR
  
  local MY_NAME_VER=\`basename \$MY_URL\`

  if [ ! -f \$MY_NAME_VER ];then
    echo "Downloading \$MY_NAME from \$MY_URL ..."
    wget \$MY_URL >& wget_\$MY_NAME-\$MY_VER.out
  fi

  echo "Unpacking \$MY_NAME with version \$MY_VER ..."
  MY_NAME_VER_NO_TARGZ=\${MY_NAME_VER%.tar.gz}
  rm -Rf \$MY_NAME_VER_NO_TARGZ
  tar xfz \$MY_NAME_VER
  MY_NAME_VER=\${MY_NAME_VER%.tar.gz}
  cd \$MY_NAME_VER
  Install_\$MY_NAME \$MY_PREFIX \`pwd\`

  cd \$MY_PREFIX
  CreateDir $PEAC_OPT_SW_BINARY_DIR
  echo "Creating tarball $PEAC_OPT_SW_BINARY_DIR/\$PEAC_SYSTEM_INFO-\$MY_NAME-\$MY_VER.tar.gz ..."
  tar cfz $PEAC_OPT_SW_BINARY_DIR/\$PEAC_SYSTEM_INFO-\$MY_NAME-\$MY_VER.tar.gz .
  
  cd \$MY_TMP_DIR
  echo "Cleaning tmp files ..."
  rm -Rf \$MY_NAME_VER
  cd \$MY_PWD

}


function Install_xrootd() {
  PEAC_OPT_NICE="nice -n 15 "
  PEAC_OPT_MAKE_OPTS="-j\`cat /proc/cpuinfo | grep processor | wc -l\`"
  echo "Compiling and installing xrootd ..."
  ./configure.classic --prefix=\$1 >& \$1/configure.out
  \$PEAC_OPT_NICE make \$PEAC_OPT_MAKE_OPTS >& \$1/make.out
  if [ "\$?" != "0" ]; then
    echo "  Error in make of xrootd in \`pwd\`"
    exit 10
  fi
  \$PEAC_OPT_NICE make \$PEAC_OPT_MAKE_OPTS install >& \$1/make_install.out
  if [ "\$?" != "0" ]; then
    echo "Error in make install of xrootd in \`pwd\`"
    exit 11
  fi
  local MY_XRD_PLATFORM=\`ls \$1/lib\`

  echo "#/bin/bash" > \$1/peac-env.sh
  echo "" >> \$1/peac-env.sh
  echo "export PATH="\$1/bin/\$MY_XRD_PLATFORM:\\\$PATH >> \$1/peac-env.sh
  echo "export LD_LIBRARY_PATH="\$1/lib/\$MY_XRD_PLATFORM:\\\$LD_LIBRARY_PATH\ >> \$1/peac-env.sh
}

function CheckSystemInfo() {

  mkdir -p /tmp/\$USER-ccache
  if [ ! -h ~/.ccache ];then 
    rm -Rf ~/.ccache
    ln -s /tmp/\$USER-ccache ~/.ccache
  fi

  local MY_SYSTEM_STRING="unsupported"
  
  local MY_RELEASE=\`cat /etc/*-release\`
  local MY_GCC_VER=\`gcc -v 2>&1 | tail -1 | awk '{print \$3}'\`
  local MY_PLATFORM=\`uname -m\`
  local MY_RELEASE_FIRST_STR=\`echo \$MY_RELEASE | awk '{print \$1}' | head -n 1\`
  if [ "\$MY_RELEASE_FIRST_STR" == "Gentoo" ];then
    MY_SYSTEM_STRING="gentoo"
  elif [ "\$MY_RELEASE_FIRST_STR" == "Scientific" ];then
    MY_SYSTEM_STRING="sl"
  elif [ "\$MY_RELEASE_FIRST_STR" == "DISTRIB_ID=Ubuntu" ];then
    MY_SYSTEM_STRING="ubuntu"
  elif [ "\$MY_RELEASE_FIRST_STR" == "Fedora" ];then
    MY_SYSTEM_STRING="fedora"
  fi

  MY_SYSTEM_STRING="\$MY_SYSTEM_STRING-\$MY_PLATFORM-\$MY_GCC_VER"

  echo "\$MY_SYSTEM_STRING"
}

function PreparePackageFromMaster() {

  if [ -f $PEAC_OPT_SW_DIR/\$2/peac-env.sh ];then
    echo "Package \$1 is already installed at $PEAC_OPT_SW_DIR/\$2/ ..."
    return
  fi

  
  if [ ! -f $PEAC_OPT_SW_BINARY_DIR/\$1 ];then
    local MY_XRD_PATH=$PEAC_OPT_SW_DIR/\$PEAC_SYSTEM_INFO/xrootd/$PEAC_XROOTD_VER/
    source \$MY_XRD_PATH/peac-env.sh
    echo "xrdcp root://$PEAC_OPT_MASTER_HOST:$PEAC_OPT_SW_XROOTD_PORT/$PEAC_OPT_SW_BINARY_DIR/\$1 $PEAC_OPT_SW_BINARY_DIR/\$1"
    xrdcp -f root://$PEAC_OPT_MASTER_HOST:$PEAC_OPT_SW_XROOTD_PORT/$PEAC_OPT_SW_BINARY_DIR/\$1 $PEAC_OPT_SW_BINARY_DIR/\$1
    MY_XRDCP_RET=\$?
    if [ "\$MY_XRDCP_RET" == "0" ];then
      echo "File was copied OK" 
    elif [ "\$MY_XRDCP_RET" == "255" ];then
      echo "File exists"
    else
      echo "xrdcp error \$MY_XRDCP_RET"
	local MY_PACKAGE_NAME=\`echo \$1 | awk -F '-' '{print \$4}'\`
	local MY_PACKAGE_VER=\`echo \$1 | awk -F '-' '{print \$5}' \`
	MY_PACKAGE_VER=\${MY_PACKAGE_VER%.tar.gz}
	echo "We need to install \$MY_PACKAGE_NAME \$MY_PACKAGE_VER..."
#       InstallPackage \$MY_PACKAGE_NAME \$MY_PACKAGE_VER 
	
      return
    fi
    echo "Return of xrdcp is \$MY_XRDCP_RET"
  fi

  mkdir -p $PEAC_OPT_SW_DIR/\$2
  ls -al $PEAC_OPT_SW_DIR/
  tar xfz $PEAC_OPT_SW_BINARY_DIR/\$1 -C $PEAC_OPT_SW_DIR/\$2

}

PEAC_SYSTEM_INFO=\$(CheckSystemInfo)
echo "system is $PEAC_SYSTEM_INFO"

if [ ! -f $PEAC_OPT_SW_DIR/\$PEAC_SYSTEM_INFO/root/$PEAC_OPT_CURRENT_ROOT_VER/peac-env.sh ];then
  MY_XRD_PATH=$PEAC_OPT_SW_DIR/\$PEAC_SYSTEM_INFO/xrootd/$PEAC_XROOTD_VER/
  mkdir -p \$MY_XRD_PATH
  echo "MY_XRD_PATH is \$MY_XRD_PATH"
  InstallPackage xrootd $PEAC_XROOTD_VER $PEAC_XROOTD_URL \$MY_XRD_PATH
  source \$MY_XRD_PATH/peac-env.sh
  PreparePackageFromMaster \$PEAC_SYSTEM_INFO-root-$PEAC_OPT_CURRENT_ROOT_VER.tar.gz \$PEAC_SYSTEM_INFO/root/$PEAC_OPT_CURRENT_ROOT_VER
fi
source $PEAC_OPT_SW_DIR/\$PEAC_SYSTEM_INFO/root/$PEAC_OPT_CURRENT_ROOT_VER/peac-env.sh

echo "MV ROOTSYS is $ROOTSYS"
EOF

cat > $PEAC_OPT_SW_DIR/xrd-simple.cf <<EOF
all.export $PEAC_SYS_DIR r/w
xrd.port $PEAC_OPT_SW_XROOTD_PORT
all.pidpath $PEAC_SYS_DIR
all.adminpath $PEAC_SYS_DIR/xrdadmin
EOF
CreateDir $PEAC_SYS_DIR/xrdadmin
}

function StartStopPodServer() {

  if [ ! -f $PEAC_OPT_SW_DIR/$PEAC_SYSTEM_INFO/root/$PEAC_OPT_CURRENT_ROOT_VER/peac-env.sh ];then
    echo "ROOT $PEAC_OPT_SW_DIR/$PEAC_SYSTEM_INFO/root/$PEAC_OPT_CURRENT_ROOT_VER/peac-env.sh is not valid !!!"
    echo "Installing ROOT ..."
    InstallRootFromVersion $PEAC_OPT_CURRENT_ROOT_VER ""
#     exit 100
  fi
  source $PEAC_OPT_SW_DIR/$PEAC_SYSTEM_INFO/root/$PEAC_OPT_CURRENT_ROOT_VER/peac-env.sh

  if [ -z $ROOTSYS ];then
      echo "ROOT is invalid or doesn't exist !!!!"
      exit 1
  fi 

  if [ ! -h $HOME/.PoD ];then
    echo "Removing $HOME/.PoD ..."
    rm -Rf $HOME/.PoD
  fi

  source  $PEAC_OPT_SW_POD_DIR/$PEAC_OPT_POD_VER/PoD_env.sh

  if [ "$1" == "start" ];then
    echo "Starting PoD ..."
    TrickPoD
    InitPoDServer
    pod-server $1

    # xrootd for sw
    source $PEAC_OPT_SW_DIR/$PEAC_SYSTEM_INFO/xrootd/$PEAC_XROOTD_VER/peac-env.sh
    echo "We are taking xrootd from `which xrootd`"
    xrootd -b -c $PEAC_OPT_SW_DIR/xrd-simple.cf -l $PEAC_OPT_LOG_DIR/xrd-software.log
    echo "Sleeping for 2 sec ..."
    sleep 2
    chmod 755 /tmp/PoD_*/*
    
    # prepare software 
#     PrepareSoftDistDir
  elif [ "$1" == "stop" ];then
    echo "Stoping PoD ..."
    pod-server $1
    if [ -f $PEAC_SYS_DIR/xrootd.pid ];then
      echo "Killing xrootd with pid `cat $PEAC_SYS_DIR/xrootd.pid` ..."
      kill `cat $PEAC_SYS_DIR/xrootd.pid` > /dev/null 2>&1
      rm -f $PEAC_SYS_DIR/xrootd.pid
    fi
  fi

  echo "Done"
}


function StarStopMLSensor() {

  if [ -z $MLSENSOR_TARGETML ];then
    return
  fi

  if [ -z $MLSENSOR_CLUSTERNAME ];then
    return
  fi

if [ "$1" == "reinstall" ];then
  if [ -f $PEAC_OPT_MLSENSOR_DIR/MLSensor/bin/MLSensor ];then
    $PEAC_OPT_MLSENSOR_DIR/MLSensor/bin/MLSensor stop
  fi
  rm -Rf $PEAC_OPT_MLSENSOR_DIR/
fi
MY_ISINSTALLING="0"
if [ ! -f $PEAC_OPT_MLSENSOR_DIR/MLSensor/bin/MLSensor ];then
  MY_ISINSTALLING="1"
  MYMLINSTALLER="MLSensor-installer-ALICE_CERN.ch"
  rm -Rf $PEAC_OPT_MLSENSOR_DIR/
  mkdir -p $PEAC_OPT_MLSENSOR_DIR/
  MYPWD=`pwd`
  cd $PEAC_OPT_MLSENSOR_DIR/
  wget -q http://monalisa.cern.ch/MLSensor/$MYMLINSTALLER
  chmod +x $MYMLINSTALLER
  export PREFIX=$PEAC_OPT_MLSENSOR_DIR
  # export MLSENSOR_TARGETML="voalice12.cern.ch"
  # export MLSENSOR_CLUSTERNAME="ALICE::CERN::CAF"
  ./$MYMLINSTALLER
  rm -f $MYMLINSTALLER
  cd $MYPWD
  
fi

if [ "$MY_ISINSTALLING" == "0" ];then 
  $PEAC_OPT_MLSENSOR_DIR/MLSensor/bin/MLSensor $1
fi
}


function SyncPeac() {
  PEAC_SRC_BASE="http://peac.googlecode.com/svn/trunk/"
}


CheckPEACConfigFile
if [ "$#" == "0" ]; then
  ShowHelp
  exit 1
fi

while [[ $1 = -* ]]; do
    arg=$1; shift

    case $arg in
        --help)
            ShowHelp
            ;;

        --install-server)
	    InstallPackage wget xrootd $PEAC_XROOTD_VER $PEAC_XROOTD_URL $PEAC_OPT_SW_DIR/$PEAC_SYSTEM_INFO/xrootd/$PEAC_XROOTD_VER/ $1
	    InstallPackage wget pod $PEAC_OPT_POD_VER $PEAC_OPT_POD_URL $PEAC_OPT_SW_DIR/$PEAC_SYSTEM_INFO/PoD/$PEAC_OPT_POD_VER/ $1
# 	    InstallPoD $1
	    shift
	    ;;

        --install-root)
	    InstallRootFromVersion $1 $2
	    shift
	    ;;
        --install-xrootd)
	    InstallPackage wget xrootd $PEAC_XROOTD_VER $PEAC_XROOTD_URL $PEAC_OPT_SW_DIR/$PEAC_SYSTEM_INFO/xrootd/$PEAC_XROOTD_VER/ $1
	    shift
	    ;;
	--all-in-one)
	    if [ -n "$2" ];then
	      PEAC_OPT_CURRENT_ROOT_VER="$2"
	    fi
	    if [ "$1" == "start" ];then
	    StartStopPodServer $1 $2
# 	    StarStopMLSensor $1 $2
	    DoPoDWorkersAction $1 $2
	    else
	    DoPoDWorkersAction $1 $2
# 	    StarStopMLSensor $1 $2
	    StartStopPodServer $1 $2
	    fi
	    shift
	    shift
            ;;
        --create-ssh-without-password)
	    CreateSSHWithoutPassword
	    shift
            ;;
        --generate-pod-workers-config)
	    GeneratePoDSSHConfig $1
	    shift
	    ;;
#         --foo)
#             do_foo "$2"
#             shift
#             ;;

	*)
	    ShowHelp
	    exit 1 
	    ;;
    esac
done
